<?php
/**
 * @version		$Id: prices.php 3 2011-12-16 20:19:15Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHosterModelPrices extends JModelList
{

	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'PriceId', 'a.PriceId',
				'PriceProduct', 'a.PriceProduct', 'product',
				'PriceBillingCycle', 'a.PriceBillingCycle', 'billing_cycle',
				'PriceAmount', 'a.PriceAmount',
				'PriceActive', 'a.PriceActive'
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{
		$app = JFactory::getApplication('administrator');
		
		$filters = JRequest::getVar('filters');
		if (empty($filters)) {
			$data = $app->getUserState($this->context.'.data');
			$filters = $data['filters'];
		}
		else {
			$app->setUserState($this->context.'.data', array('filters'=>$filters));
		}
		
		$productId = $app->getUserStateFromRequest($this->context.'.filter.product_id', 'filter_product_id');
		$this->setState('filter.product_id', $productId);
		
		$billingcycleId = $app->getUserStateFromRequest($this->context.'.filter.billingcycle_id', 'filter_billingcycle_id');
		$this->setState('filter.billingcycle_id', $billingcycleId);
		
		parent::populateState('a.PriceProduct', 'asc');		
				
	}

	
	protected function getListQuery() 
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		
		$query->select($this->getState('list.select', 'a.*'));
		
		$query->from('#__almh_prices AS a');
		
		$query->select('p.ProductName AS product');
		$query->join('LEFT', '#__almh_products AS p ON p.ProductId = a.PriceProduct');
		
		$query->select('b.BillcycleName AS billing_cycle');
		$query->join('LEFT', '#__almh_billingcycles AS b ON b.BillcycleMonths = a.PriceBillingCycle');
		
		$productId = $this->getState('filter.product_id');
		if (is_numeric($productId)) {
			$query->where('a.PriceProduct = '.$productId);
		}
		
		$billingcycleId = $this->getState('filter.billingcycle_id');
		if (is_numeric($billingcycleId)) {
			$query->where('a.PriceBillingCycle = '.$billingcycleId);
		}
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.PriceProduct')).' '.$db->getEscaped($this->getState('list.direction', 'asc')));
			
		return $query;		
	}
	
	public function getProduct() {
		
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		$query->select('p.ProductId AS value, p.ProductName AS text');
		$query->from('#__almh_products AS p');
		$query->join('INNER', '#__almh_prices AS o ON o.PriceProduct = p.ProductId');
		$query->group('p.ProductId');
		$query->order('p.ProductName');

		$db->setQuery($query->__toString());

		return $db->loadObjectList();
	}
	
	public function getBillingCycle() {
		
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		$query->select('b.BillcycleMonths AS value, b.BillcycleName AS text');
		$query->from('#__almh_billingcycles AS b');
		$query->join('INNER', '#__almh_prices AS o ON o.PriceBillingCycle = b.BillcycleMonths');
		$query->group('b.BillcycleMonths');
		$query->order('b.BillcycleName');

		$db->setQuery($query->__toString());

		return $db->loadObjectList();
	}
}